<?php
namespace App\Lib\Consul;

/**
 * 用户传递来的 service 参数
 * service 参数 参数格式为： Prefix.ServiceName.Server.Action
 * Class ServiceInfo
 * @package App\Http\Lib
 */
class ServiceInfo implements ServiceInfoInterface
{

    /**
     * 处理后生成的数组
     * @var array
     */
    private $serviceData = [];

    /**
     * 用户传递进来的参数 没有经过处理的
     * @var string
     */
    private $paramService = '';

    /**
     * 服务前缀
     * @var string
     */
    private $prefix = '';

    /**
     * 服务名称
     * @var string
     */
    private $serviceName = '';

    /**
     * 服务中目标类名称
     * @var string
     */
    private $server = '';

    /**
     * 服务中目标方法名称
     * @var string
     */
    private $action = '';

    /**
     * 类是否有效
     * @var bool
     */
    private $useful = false;

    public function __construct(string $service)
    {

        $this->paramService = $service;

        // 过滤 空和多余的 .
//        $service = trim(trim(trim($service, ' '), '.'), ' ');

        // 转化成数组 并检测每个单元的数量
        $this->serviceData = explode('.',$service);

        // 初始化到属性中
        [$this->prefix, $this->serviceName, $this->server, $this->action] = $this->serviceData;
        $this->useful = true;
    }

    /**
     * @return array
     */
    public function getServiceData(): array
    {
        return $this->serviceData;
    }

    /**
     * @return string
     */
    public function getParamService(): string
    {
        return $this->paramService;
    }

    /**
     * @return string
     */
    public function getPrefix(): string
    {
        return $this->prefix;
    }

    /**
     * @return string
     */
    public function getServer(): string
    {
        return $this->server;
    }

    /**
     * @return string
     */
    public function getAction(): string
    {
        return $this->action;
    }

    /**
     * @inheritDoc
     */
    public function getServiceName(): string
    {
        return $this->serviceName;
    }

    /**
     * @inheritDoc
     */
    public function getFullName(): string
    {
        //当是hyperf的框架的时候就去无间隙的去取
        $split = '_';
        if(strtolower($this->prefix) == 'hyperf'){
            return $this->serviceName;
        }
        return $this->prefix . $split . $this->serviceName;
    }

    public function useful(){
        return $this->useful;
    }


}
